<script type="text/javascript">

var		selected = null;
var		good_squares;

function RemoveValidSquares () {
	
	$('.cb_square_selected').removeClass ( 'cb_square_selected' );
	$('.cb_valid_move_square').removeClass ( 'cb_valid_move_square' );
	
	for (var i=0; i < good_squares.length; i++) {
		//var tmp = $('#cb_valid_move_quare_background_' + good_squares[i]).html ();
		//$("#${chessgameid}_location_"+good_squares[i]).html (tmp);
		//$('#cb_valid_move_quare_background_' + good_squares[i]).remove ();
		//$('#cb_valid_move_quare_background_' + good_squares[i]).empty ();
		$("#${chessgameid}_location_filter_"+good_squares[i]).removeClass ( "cb_valid_move_square_filter" );
		$("#${chessgameid}_location_filter_"+good_squares[i]).addClass ( "cb_invalid_move_square_filter" );
	}

	selected = null;	
	
}

$('.cb_valid_move_square').live ( 'click', function () {
	
	var		floc = $(selected).attr("id");
	var		tloc = $(this).attr("id");
	
	floc = floc.substr ( floc.length - 2, 2 );
	tloc = tloc.substr ( tloc.length - 2, 2 );
	
	$.ajax({
		  url: "${root_path}/AjaxMovePiece",
		  type: "POST",
		  data: "chessboard_id=${chessboardID}&from=" + floc + "&to=" + tloc,
		  cache: false,
		  success: function(html){
			  
			  var t = html;
			  
			  if ( t == "true" )
			  {
					$.ajax({
						  url: "${root_path}/AjaxChessBoard",
						  type: "POST",
						  data: "chessboard_id=${chessboardID}",
						  cache: false,
						  success: function(html){
							  $("#page_content").empty();
							  $("#page_content").html ( html );
						  }
						});	
			  }else
			  {
				  $('#ajax_err').html ( html );
			  }
			
		  }
		});	
	
	RemoveValidSquares();
});

$('.cb_valid_select_piece').live('click', function () {	

	var		loc = $(this).attr("id");
	
	loc = loc.substr ( loc.length - 2, 2 );
	
	if ( selected != null )
	{
	
		if ( selected != this )
		{
			return;		
		}		
		
		//
		
		RemoveValidSquares();

		return;
	}
		
	$('.cb_square_selected').removeClass ( 'cb_square_selected' );
	$(this).addClass ( 'cb_square_selected' );
	
	$.ajax({
		  url: "${root_path}/AjaxMoveRequest",
		  type: "POST",
		  data: "chessboard_id=${chessboardID}&from=" + loc,
		  cache: false,
		  success: function(html){

			  var s=html;
			  good_squares=s.split(new RegExp("[,]","g"));
			  
			  for (var i=0; i< good_squares.length; i++) {
				  	good_squares[i] = $.trim(good_squares[i]);
					$("#${chessgameid}_location_"+good_squares[i]).addClass ( "cb_valid_move_square" );
					$("#${chessgameid}_location_filter_"+good_squares[i]).removeClass ( "cb_invalid_move_square_filter" );
					$("#${chessgameid}_location_filter_"+good_squares[i]).addClass ( "cb_valid_move_square_filter" );					
					//var h = $('#${chessgameid}_location_'+good_squares[i]).html();
					//$("#${chessgameid}_location_"+good_squares[i]).html ( '<div id="cb_valid_move_quare_background_' + good_squares[i] + '" class="cb_valid_move_square_background">' + h + '</div>' );
			  }
			  
		  }
		});		
	
	selected = this;

});

</script>